<?php
class MapManagementModel extends SBKH{	
	function getRecords($pro_id = 0,$search = ""){
		$sql = "
		SELECT * 
		FROM " . $this -> GetPrefix('province') . " as t				
		";
		if($pro_id != 0 && $search == ""){
			$sql .= " WHERE `id` = '" . $pro_id . "'";
		}elseif($pro_id == 0 && $search != ""){
			$sql .= " WHERE `province_name` LIKE '%" . $search . "%'";
		}elseif($pro_id != 0 && $search != ""){
			$sql .= " WHERE `id` = '" . $pro_id . "' && `province_name`= '%" . $search . "%'";
		}
		$result = $this->query($sql);
		return $result;
	}
		
	function deleteProvince($pro_id = 0){
		$sql = "
		DELETE FROM " . $this -> GetPrefix('province') . "
		WHERE `id` = '" . $pro_id . "'
		";
		return $this->query($sql);
	}	
	
	function getDistrictRecords($pro_id = 0, $dis_id = 0,$search = "", $start = 0, $end = 15){
		$sql = "
		SELECT *,
				(SELECT `province_name` FROM " . $this -> GetPrefix('province') . " as p WHERE d.`province_id` = p.`id`) as province_name 
		FROM " . $this -> GetPrefix('district') . " as d
		";
		$where = "";
		if($dis_id != 0 && $search == ""){
			$where .= " WHERE `id` = '" . $dis_id . "'";
		}elseif($dis_id == 0 && $search != ""){
			$where .= " WHERE `district_name` LIKE '%" . $search . "%'";
		}elseif($dis_id != 0 && $search != ""){
			$where .= " WHERE `id` = '" . $dis_id . "' && `district_name`= '%" . $search . "%'";
		}
		if($pro_id != 0){
			if($where != ""){
				$where .= " AND `province_id` = '" . $pro_id . "'";
			}else{
				$where .= " WHERE `province_id` = '" . $pro_id . "'";
			}
		}
		
		$sql .= $where;
		
		if($end == 0){
			$sql .= " GROUP BY d.`district_name` ORDER BY  `province_id` DESC";
		}else{
			$sql .= " GROUP BY d.`district_name` ORDER BY  `province_id` DESC LIMIT $start,$end";
		}			
		$result = $this->query($sql);
		/* For Pagination */
		$sql = "SELECT COUNT(d.`id`) as number_of_rows FROM " . $this -> GetPrefix('district') . " as d";
		$sql .= $where;
		$result2 = $this->query($sql);
		return array($result,$result2[0]->number_of_rows);
	}	
	
	function deleteDistrict($dis_id = 0){
		$sql = "
		DELETE FROM " . $this -> GetPrefix('district') . "
		WHERE `id` = '" . $dis_id . "'
		";
		return $this->query($sql);
	}
}
?>